package de.g18.scavengerhunt.overlay;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.OverlayItem;

import de.g18.scavengerhunt.AppApplicationContext;
import de.g18.scavengerhunt.R;
import de.g18.scavengerhunt.utils.GameUtils;

/**
 * This overlay displays all user on the map
 */
public class UserOverlay extends GraphicOverlay {

    private Context context = null;

    public UserOverlay(Drawable drawable, Context context) {
        super(boundCenterBottom(drawable));
        this.context = context;
        populate();
    }

    @Override
    protected boolean onTap(int index) {
        OverlayItem item = overlayItems.get(index);

        int distance = 0;
        if (AppApplicationContext.getUser().getCurrentGeoPoint() != null && item.getPoint() != AppApplicationContext.getUser().getCurrentGeoPoint()) {
            distance = GameUtils.getDistanceBetweenPoints(AppApplicationContext.getUser().getCurrentGeoPoint(), item.getPoint());
        }

        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet() + "\n" + context.getString(R.string.distance) + ": " + String.valueOf(distance) + " " + context.getString(R.string.meters) + ".");
        dialog.show();

        return true;
    }

      @Override
    protected OverlayItem createItem(int i) {
        return overlayItems.get(i);
    }

     @Override
    public int size() {
        return overlayItems.size();
    }

    public void clear() {
        overlayItems.clear();
    }
}
